package com.example.oms.service.impl;

import com.example.oms.dao.MedsDao;
import com.example.oms.dao.PrescriptionsDao;
import com.example.oms.vo.MedVo;
import com.example.oms.vo.PrescriptionAndMedListVo;
import com.example.oms.entity.Meds;
import com.example.oms.service.PharmacyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class PharmacyServiceImpl implements PharmacyService {
    @Resource
    MedsDao medsDao;
    @Resource
    PrescriptionsDao prescriptionsDao;

    @Override
    public void addStock(Integer medId) {
        medsDao.updateStock(medId, 1);
    }

    @Override
    public void clearStock(Integer medId) {
        medsDao.clearStock(medId);
    }

    @Override
    public List<Meds> getAllDrugs() {
        return medsDao.getAllDrugs();
    }

    @Override
    public List<PrescriptionAndMedListVo> getAllPrescription() {
        List<Map<String, Object>> allPaidOrCompletedPrescriptionsWithMedList =
                prescriptionsDao.getAllPaidOrCompletedPrescriptionsWithMedList();
        Map<Integer, PrescriptionAndMedListVo> resultMap = new HashMap<>();

        for (Map<String, Object> map : allPaidOrCompletedPrescriptionsWithMedList) {
            Integer presId = (Integer) map.get("presId");
            if (!resultMap.containsKey(presId)) {
                PrescriptionAndMedListVo prescriptionAndMedListVo = new PrescriptionAndMedListVo();
                prescriptionAndMedListVo.setId(presId);
                prescriptionAndMedListVo.setDoctorId((Integer) map.get("docId"));
                prescriptionAndMedListVo.setPatientId((Integer) map.get("patientId"));
                prescriptionAndMedListVo.setStatus((String) map.get("status"));
                prescriptionAndMedListVo.setTime((Date) map.get("time"));
                prescriptionAndMedListVo.setRemarks((String) map.get("remarks"));
                prescriptionAndMedListVo.setDiagRecordId((Integer) map.get("diagRecordId"));
                prescriptionAndMedListVo.setMedsList(new ArrayList<>());
                resultMap.put(presId, prescriptionAndMedListVo);
            }
            MedVo med = new MedVo();
            med.setId((Integer) map.get("medId"));
            med.setName((String) map.get("medName"));
            med.setPrice(((java.math.BigDecimal) map.get("medPrice")).floatValue());
            med.setStock((Integer) map.get("medStock"));
            med.setSpecification((String) map.get("medSpecification"));
            med.setQuantities((Integer) map.get("quantities"));

            if(resultMap.get(presId).getMedsList()!=null){
                resultMap.get(presId).getMedsList().add(med);
            }
        }

        return new ArrayList<>(resultMap.values());
    }

    @Override
    public void Distribute(Integer prescriptionId) {
        List<MedVo> medList = medsDao.getMedListByPresId(prescriptionId);
        prescriptionsDao.changePrescriptionStatus(prescriptionId, "Completed");
        for (MedVo medVo : medList) {
            medsDao.updateStock(medVo.getId(), -medVo.getQuantities());
        }
    }

    @Override
    public void withdrawDrugs(Integer prescriptionId) {
        List<MedVo> medList = medsDao.getMedListByPresId(prescriptionId);
        prescriptionsDao.changePrescriptionStatus(prescriptionId, "Canceled");
        for (MedVo medVo : medList) {
            medsDao.updateStock(medVo.getId(), medVo.getQuantities());
        }
    }
}
